﻿//==============================================================================
// File: TabMenuItemWindow.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arek Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WpfRcp.Controls.Behaviors;

namespace WpfRcp.Controls
{
    [TemplatePart(Name = PART_Attach, Type = typeof(Button))]
    public class TabMenuItemWindow : ChromeWindow
    {
        private const string PART_Attach = "PART_Attach";
        private TabMenu m_TabMenu;
        private TabMenuItem m_MenuItem;
        private bool m_IsDocking;

        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(TabMenuItemWindow));

        public static readonly RoutedEvent AttachEvent = EventManager.RegisterRoutedEvent("Attach", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(TabMenuItemWindow));

        public static readonly RoutedEvent ContentCloseEvent = EventManager.RegisterRoutedEvent("ContentClose", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(TabMenuItemWindow));

        static TabMenuItemWindow()
        {
            // This OverrideMetadata call tells the system that this element wants
            // to provide a style that is different than its base class
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TabMenuItemWindow),
                                                     new FrameworkPropertyMetadata(typeof(TabMenuItemWindow)));

            EventManager.RegisterClassHandler(typeof(TabMenuItemWindow), WindowBehavior.HiddenEvent, (RoutedEventHandler)HiddenEventHandler);
            
        }

        private static void HiddenEventHandler(object d, RoutedEventArgs handler)
        {
            var w = (TabMenuItemWindow)d;
            w.RaiseContentCloseEvent();
        }

        public TabMenuItemWindow()
        {
            var binding = new Binding("Title") { Source = this };
            BindingOperations.SetBinding(this, HeaderProperty, binding);

            Closed += ((sender, e) => RaiseContentCloseEvent());
        }

        public TabMenuItemWindow(TabMenu tabMenu, TabMenuItem menuItem)
        {
            TabMenu = tabMenu;
            MenuItem = menuItem;

            Header = MenuItem.Header;
            IsCloseButtonVisible = MenuItem.IsCloseable;
        }

        public event RoutedEventHandler Attach
        {
            add { AddHandler(AttachEvent, value); }
            remove { RemoveHandler(AttachEvent, value); }
        }

        public event RoutedEventHandler ContentClose
        {
            add { AddHandler(ContentCloseEvent, value); }
            remove { RemoveHandler(ContentCloseEvent, value); }
        }

        private void RaiseAttachEvent()
        {
            m_IsDocking = true;
            RaiseEvent(new RoutedEventArgs(AttachEvent, this));
        }

        private void RaiseContentCloseEvent()
        {
            if (m_IsDocking)
                m_IsDocking = false;
            else
                RaiseEvent(new RoutedEventArgs(ContentCloseEvent));
        }

        public TabMenu TabMenu
        {
            get { return m_TabMenu; }
            set { m_TabMenu = value; }
        }

        public TabMenuItem MenuItem
        {
            get { return m_MenuItem; }
            set { m_MenuItem = value; }
        }

        public object Header
        {
            get { return GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            Button button = GetTemplateChild(PART_Attach) as Button;
            if (button != null)
                button.Click += OnAttachClicked;
        }

        private void OnAttachClicked(object sender, RoutedEventArgs e)
        {
            RaiseAttachEvent();
        }
    }
}